package edu.faytechcc.csc251.elementalTD.towers;

import edu.faytechcc.csc251.elementalTD.*;
import java.awt.*;
import java.util.*;

/**
 * Tower class stores the tower classes, info, doesn't function how I expected
 * that is why it is being cloned. 
 * @author William Robles
 */
public class Tower implements Cloneable
{
    
    public Image texture;
    
    public static final Tower[] towerList = new Tower[6];
    
    public static final Tower arrowTower = new ArrowTower(  0, 7, 6, 6, 0, 200).getTextureFile(0);
    public static final Tower cannonTower = new CannonTower(1, 9, 5, 9, 0, 500).getTextureFile(1);
    public static final Tower airTower = new AirTower(      2, 12, 9, 20, 1, 200).getTextureFile(2);
    
    public int towerX = 0;
    public int towerY = 0;
    
    public int id;
    public int cost;
    public int range;
    public int damage;
    
    public Mob target = null;
    
    public int attackCounter;
    public int attackReload;
    
    public int MaxAttackCounter;
    public int MaxAttackReload;
    
    private final int ATTACK = 0;
    private final int ATTACK_CLOSEST_EXIT = 1;
    private final int ATTACK_ANY = 2;
    
    public int attackAI = ATTACK;
    
    public boolean selected;
    
    public Tower(int id, int cost, int range, int damage, int MaxAttackCounter, int MaxAttackReload)
    {
        
        if (towerList[id] != null)
        {
            System.out.println("Two towers with same id! id = " + id);
        }
        else
        {
            towerList[id] = this;
            
            this.id = id;
            this.cost = cost;
            this.range = range;
            this.damage = damage;
            
            this.MaxAttackCounter = MaxAttackCounter;
            this.MaxAttackCounter = MaxAttackReload;
            attackCounter = 0;
            attackReload = 0;
            
        }
    }
    
    /**
     * getTextureFile assigns image to tower
     * @param id is id of tower 
     * @return null
     */
    public Tower getTextureFile(int id)
    {
        this.texture = GameDisplay.upperLevel[id];
        
        return null;
    }

    /**
     * targetedEnemy method picks the enemy to shoot
     *
     * @param mobs arraylist
     * @param y
     * @param x
     * @return Mob the mob
     */
    public Mob targetedEnemy(ArrayList mobs, int y, int x)
    {
        ArrayList<Mob> mobInRange = new ArrayList<>();
        
        towerX = GameDisplay.map.block[y][x].x;
        towerY = GameDisplay.map.block[y][x].y;

        
        int mobX = 0;
        int mobY = 0;
        
        int towerRadius = this.range;
        
        
        if (mobs != null)
        {
            ArrayList<Mob> mobsInGame = mobs;
            
            for (Mob mob : mobsInGame)
            {
                mobX = mob.x;
                mobY = mob.y;
                int dx = mobX - towerX;
                int dy = mobY - towerY;
                double distance = Math.sqrt((dx * dx) + (dy * dy));
                double diameter = (double)((towerRadius+ .50) * (double)(GameDisplay.map.blockSize/2));
                
                if ( distance < diameter)
                {
                    mobInRange.add(mob);
                }
            }
            // Randomly chooses a mob
            if (this.attackAI == ATTACK_ANY)
            {   
                if (!mobInRange.isEmpty())
                {
                    Mob temp = mobInRange.get(new Random().nextInt(mobInRange.size()));
                    mobInRange.remove(temp);
                    return temp;
                }
                
            }
            else if(this.attackAI == ATTACK)
            {
                if (!mobInRange.isEmpty())
                {   
                    for(Mob mob:mobInRange)
                    {
                        Mob temp = mob;
                        mobInRange.remove(temp);
                        return temp;
                    }
                    
                }
            }
        }
        return null;
        
    }
    
     @Override
    public Object clone() throws CloneNotSupportedException
    {
        try
        {
            return super.clone();
        }
        catch (CloneNotSupportedException e)
        {
            System.out.println("Hey!!! I'm Not Cloning");
        }
        
        return null;
    }
    
}